﻿using System.Collections.Generic;
using System.Linq;
using SnippetBox.Core.Data.Context;
using SnippetBox.Core.Data.Models;
using SnippetBox.Core.Data.Repository;

namespace SnippetBox.Core.Data.Service {
    public abstract class ServiceBase<T> : IServiceBase<T> where T : class, IDeletable {
        protected readonly IRepository<T> _repository;
        
        protected ServiceBase() {
            var context = ContextHelper<DatabaseContext>.GetCurrentContext();
            _repository = new Repository<T>(context);
        }

        public virtual List<T> GetAll() {
            return _repository.GetAll().ToList();
        }

        public T GetById(int entityId) {
            return _repository.GetById(entityId);
        }

        public void Create(T entity) {
            _repository.Add(entity);
        }

        public void Delete(T entity) {
            _repository.Delete(entity);
        }

        public void Save() {
            _repository.SaveChanges();
        }
    }
}